package com.jokins.admin.ios.user.action;

import com.jokins.admin.ios.user.actioncontext.BookUserRegisterPasswordDoc;
import com.jokins.admin.ios.user.mapper.BookUserMapper;
import com.jokins.admin.ios.user.param.BookUserRegisterPasswordParam;
import com.jokins.admin.vue.user.utils.FixedHashPasswordUtils;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.entry.ios.user.BookUser;
import com.jokins.common.util.SqlTypeConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2025/2/5
 */
@Slf4j
@Component
public class BookUserRegisterPasswordDocAction extends BaseAction<BookUserRegisterPasswordDoc> {

    @Autowired
    private BookUserMapper bookUserMapper;

    @Override
    protected void process(BookUserRegisterPasswordDoc actionContext) throws BusinessException {
        BookUserRegisterPasswordParam param = actionContext.getParam();
        BookUser bookUser = new BookUser();
        String sourcePassword = param.getPassword();
        String newPassword = FixedHashPasswordUtils.sha256Hash(param.getPassword());
        bookUser.setPassword(sourcePassword);
        bookUser.setSalt(newPassword);
        bookUser.setAccount(param.getAccount());

        SqlTypeConverter.copyEntity(param,bookUser);
        bookUserMapper.insert(bookUser);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("password",sourcePassword);
        resultMap.put("userName",newPassword);
        actionContext.setResult(ActionContext.RESULT_DOC_KEY,resultMap);
    }
}